package com.example.myapplication.room;

import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.example.myapplication.MyApplication;
import com.example.myapplication.R;
import com.example.myapplication.goldfish.GoldfishActivity;
import com.example.myapplication.mvvm.MainActivity;
import com.example.myapplication.rxjava.RXJava;

import java.util.List;

public class RoomActivity extends AppCompatActivity {
    RequestCollectViewModel requestCollectViewModel;
//    String[] name = {"张三", "李四", "王四", "李三", "阿四", "老王", "老五", "哥四", "呵呵中", "嘌呤戌"};
//    int[] age = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};

    List<Student> list;
    String name = "老王";
    int item = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room);

        RecyclerView recyclerView = findViewById(R.id.room_recyclerview);
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(mLinearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        requestCollectViewModel = new RequestCollectViewModel(MyApplication.getInstance());
        requestCollectViewModel.getStudentsLive().observe(this, new Observer<List<Student>>() {
            @Override
            public void onChanged(List<Student> studentList) {
                list = studentList;
                AtadaperRoom atadaper = new AtadaperRoom(studentList);
                recyclerView.setAdapter(atadaper);
            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
            }
        }).start();


        findViewById(R.id.tv_textview_5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item++;
//                for (int i = 0; i < name.length; i++) {
                requestCollectViewModel.touchoffInsertStudents(new Student(name + "    " + item, item));
//                }

            }
        });


        findViewById(R.id.tv_textview_6).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestCollectViewModel.touchoffdeleteAllStudents();
            }
        });

        findViewById(R.id.tv_textview_7).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.size() > 0) {
                    requestCollectViewModel.touchoffDeleteStudents(list.get(0));
                }
            }
        });

        findViewById(R.id.tv_textview_8).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RoomActivity.this, GoldfishActivity.class);
                startActivity(intent);
            }
        });
        findViewById(R.id.tv_textview_9).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RoomActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });

        RXJava.getData();
    }
}